<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { showMsg } from '../../utils/showMsg';
import { REGISTER_API } from '../../apis/register';

const router = useRouter()
const formRef = ref(null)
const isAgree = ref(true)
const  generateRandomFourDigits= () => Math.floor(1000 + Math.random() * 9000);
const code = generateRandomFourDigits()

const formData = ref({
  username: 'admin2',
  password: '123456',
  code: code
})

const rules = {
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 3, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 10,message: '长度在 6 到 10 个字符', trigger: ''}
  ],
  code: [
    { required: true, message: '请输入验证码', trigger: 'blur' }
  ]
}
const handleCancel = () => { router.push('/login'); showMsg('已取消', 'info')}
const handleRegister = async() => {
  if(isAgree.value === false) showMsg("请勾选同意",'warning')
  if(formData.value.code != code) showMsg("验证码错误",'warning')
  if(isAgree.value && formData.value.code == code){
    await formRef.value.validate(valid => {
      if(valid){
        REGISTER_API(formData.value).then(res => {
          // console.log(res)
          showMsg('注册成功','success')
          router.push('/login')
        })
      }else{ showMsg('注册失败','warning') }
    })
  }
}
</script>
<template>
  <div class="container">
    <div class="main">
      <div class="title"><p>系统注册</p></div>
      <div class="form">
        <el-form :rules="rules" :model="formData" ref="formRef">
          <el-form-item prop="username"><el-icon><User /> </el-icon><el-input placeholder="请输入账号" v-model="formData.username"/></el-form-item>
          <el-form-item prop="password"><el-icon><Check /></el-icon><el-input placeholder="请输入密码" v-model="formData.password" type="password" show-password/></el-form-item>
          <div class="code">
            <el-form-item style="margin-left: 34px;" prop="code"><el-input placeholder="验证码" style="flex: 1;width: 100px;" v-model="formData.code"/></el-form-item>
            <div style="flex: 1;width: 100px;margin-top: 8px;">{{ code }}</div>
          </div>
          <div class="agree">
            <el-checkbox label="同意协议" v-model="isAgree"></el-checkbox>
          </div>
          <div class="btn">
            <el-button type="primary" @click="handleRegister">注册</el-button>
            <el-button type="default" @click="handleCancel">取消</el-button>
          </div>
        </el-form>
      </div>
    </div>
 </div>
</template>
<style scoped>
.container{
  width: 100vw;
  height: 100vh;
  background-color: bisque;
  background-image: url('src/assets/star2.png');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
}
.main{
  width: 26%;
  height: 50%;
  background-color: #f4f4f4;
}
.title{
  text-align: center;
  font-size: 30px;
}
.form{
  display: flex;
  justify-content: center;
  align-items: center;
}
.el-input{
  width: 300px;
  height: 34px;
}
.el-icon{
  font-size: 24px;
  margin-right: 10px;
}
.code{
  display: flex;
}
.el-form-item{
  margin-bottom: 30px;
}
.agree{
  margin: 10px;
  margin-bottom: 20px;
}
.el-button{
  width: 110px;
  height: 35px;
  margin-left: 30px;
}
</style>